import { defineStore } from 'pinia'
import { ref } from 'vue'
//用户模块token setToken removeToken
export const useAllUsersStore = defineStore('big-allUsers', () => {
  /*
  createTime
: 
"2024-06-07T18:06:54"
gender
: 
1
id
: 
3
image
: 
null
name
: 
"admin"
password
: 
"123456"
phone
: 
"12345678911"
role
: 
null
updateTime
: 
"2024-06-07T18:06:54"
username
: 
"admin"
  */
  //全部用户的表
  const allUsersList = ref([])
  const allUnreadMessage = ref({})
  const addAllUsers = (newUsers) => {
    allUsersList.value.push(newUsers)
    console.log('目前网站总共用户是:')
    console.log(allUsersList.value)
  }
  const initAllUsers = (newUsersList) => {
    allUsersList.value = newUsersList
    for (let item in newUsersList) {
      allUnreadMessage.value[item.userName] = ''
    }
  }
  const initAllUnreadMessage = (obj) => {
    console.log('obj:---::::---:::')
    console.log(obj)
    for (let key in obj) {
      console.log(key)
      allUnreadMessage.value[key] = Number(obj[key])
      console.log(allUnreadMessage.value[key])
    }
  }
  const removeAllUsers = (userName) => {
    allUsersList.value = allUsersList.value.filter(
      (item) => item.userName !== userName
    )
    ElMessage({
      message: `用户${userName}已注销`,
      type: 'error',
      plain: true
    })
  }
  const findUser = (userName) => {
    console.log('*******----------**************-----------------------')
    console.log(allUsersList)
    console.log(allUsersList.value)
    for (let i = 0; i < allUsersList.value.length; i++) {
      if (allUsersList.value[i].username === userName) {
        return allUsersList.value[i]
      }
    }
  }
  const refreshList = () => {
    allUsersList.value.push({
      createTime: '2024-06-07T18:06:54',
      gender: 1,
      id: 3,
      image: null,
      name: 'admin',
      password: '123456',
      phone: '12345678911',
      role: null,
      updateTime: '2024-06-07T18:06:54',
      username: 'admin'
    })
    allUsersList.value.pop()
    console.log('出去又出来了');
  }
  allUsersList
  return {
    allUsersList,
    allUnreadMessage,
    addAllUsers,
    removeAllUsers,
    initAllUsers,
    findUser,
    initAllUnreadMessage,
    refreshList
  }
})
